import apiClient from './api'

// API配置接口
export interface APIConfig {
  baseUrl: string
  timeout: number
  retryCount: number
  apiKey: string
  useProxy: boolean
  proxySettings?: {
    host: string
    port: number
    username?: string
    password?: string
  }
}

// 模型配置接口
export interface ModelConfig {
  model: string
  temperature: number
  maxTokens: number
  topP: number
  frequencyPenalty: number
  presencePenalty: number
  stopSequences: string[]
  contextWindow: number
  enabled: boolean
  default: boolean
}

// RAG配置接口
export interface RAGConfig {
  enabled: boolean
  topK: number
  similarityThreshold: number
  chunkSize: number
  chunkOverlap: number
  embeddingModel: string
  rerankingEnabled: boolean
  rerankingModel: string
  rerankingTopK: number
}

// UI配置接口
export interface UIConfig {
  theme: 'light' | 'dark' | 'system'
  fontSize: number
  fontFamily: string
  showSidebar: boolean
  sidebarWidth: number
  showRightPanel: boolean
  rightPanelWidth: number
  showLineNumbers: boolean
  codeTheme: string
  animationSpeed: 'slow' | 'normal' | 'fast'
}

// 系统设置接口
export interface SystemSettings {
  api: APIConfig
  models: ModelConfig[]
  rag: RAGConfig
  ui: UIConfig
  general: {
    language: string
    autoSave: boolean
    clearCacheOnExit: boolean
    checkUpdates: boolean
    telemetry: boolean
    timezone: string
  }
  security: {
    autoLogout: boolean
    autoLogoutTime: number
    encryptionEnabled: boolean
    dataRetentionDays: number
  }
  integrations: {
    [key: string]: {
      enabled: boolean
      [key: string]: any
    }
  }
}

// 设置服务类
class SettingsService {
  // 获取所有设置
  async getSettings(): Promise<SystemSettings> {
    const response = await apiClient.get('/v1/settings')
    return response.data
  }

  // 更新设置
  async updateSettings(settings: Partial<SystemSettings>) {
    const response = await apiClient.put('/v1/settings', settings)
    return response.data
  }

  // 重置设置为默认值
  async resetSettings() {
    const response = await apiClient.post('/v1/settings/reset')
    return response.data
  }

  // 导入设置
  async importSettings(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    
    const response = await apiClient.post('/v1/settings/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return response
  }

  // 导出设置
  async exportSettings() {
    const response = await apiClient.get('/v1/settings/export', {
      responseType: 'blob'
    })
    return response
  }

  // 获取API配置
  async getAPIConfig(): Promise<APIConfig> {
    const response = await apiClient.get('/v1/settings/api')
    return response.data
  }

  // 更新API配置
  async updateAPIConfig(config: APIConfig) {
    const response = await apiClient.put('/v1/settings/api', config)
    return response.data
  }

  // 获取模型配置列表
  async getModelConfigs(): Promise<ModelConfig[]> {
    const response = await apiClient.get('/v1/settings/models')
    return response.data
  }

  // 添加或更新模型配置
  async saveModelConfig(config: ModelConfig) {
    const response = await apiClient.post('/v1/settings/models', config)
    return response
  }

  // 删除模型配置
  async deleteModelConfig(modelId: string) {
    const response = await apiClient.delete(`/v1/settings/models/${modelId}`)
    return response
  }

  // 获取RAG配置
  async getRAGConfig(): Promise<RAGConfig> {
    const response = await apiClient.get('/v1/settings/rag')
    return response.data
  }

  // 更新RAG配置
  async updateRAGConfig(config: RAGConfig) {
    const response = await apiClient.put('/v1/settings/rag', config)
    return response.data
  }

  // 获取UI配置
  async getUIConfig(): Promise<UIConfig> {
    const response = await apiClient.get('/v1/settings/ui')
    return response.data
  }

  // 更新UI配置
  async updateUIConfig(config: UIConfig) {
    const response = await apiClient.put('/v1/settings/ui', config)
    return response
  }

  // 检查系统状态
  async checkSystemStatus() {
    const response = await apiClient.get('/v1/settings/status')
    return response
  }

  // 获取系统信息
  async getSystemInfo() {
    const response = await apiClient.get('/v1/settings/system-info')
    return response
  }
}

export default new SettingsService()